#Defaults ....
Timeouts=0
LinkDelay=0
CallCost=0
Calls=1
Sanity="false"
instrument="false"

Nodes=1
Read=10
Degree=3
Bench="bank"
Objects=10
Threads=1
Transactions=1
Verbose=5
Debug="false"
Machine_Index=0
Machines=1

TransactionLength=0
ClusterWaitTime=200000
Reap="false"
ReapedNodes="0"
JLogger="fatal"
ContentionPolicy="Priority"

FreeCore=0

#Parameters ....

while getopts :T:K:C:f:c:s:i:n:r:D:b:o:t:x:v:d:I:M:L:cW:rP:rN:jL:cP: option
do
	case "$option" in
	T)	Timeouts="$OPTARG";;
	K)	LinkDelay="$OPTARG";;
	C)	CallCost="$OPTARG";;
	f)	FreeCore="$OPTARG";;
	c)  Calls="$OPTARG";;
	s)	Sanity="true";;
	i)	instrument="true";;
	n)	Nodes="$OPTARG";;
	r)	Read="$OPTARG";;
	D)  Degree="$OPTARG";;
	b)	Bench="$OPTARG";;
	o)	Objects="$OPTARG";;
	t)	Threads="$OPTARG";;
	x)	Transactions="$OPTARG";;
	v)	Verbose="$OPTARG";;
	d)	Debug="true";;
	I)	Machine_Index="$OPTARG";;
	M)	Machines="$OPTARG";;
	L)  TransactionLength="$OPTARG";;
	cW) ClusterWaitTime="$OPTARG";;
	rP) Reap="true";;
	rN) ReapedNodes="$OPTARG";;
	jL) JLogger="$OPTARG";;
	cP) ContentionPolicy="$OPTARG";;
	[?]) 	echo "Usage: $0 [-b benchmark] [-n nodes] [-r reads%] [-o objects] [-t threads] [-x transactions] [-v] [-d] [-s] [-i] [-D] [-M machines] [-I machine_index] [-T timeout] [-L transaction_length] [-K link_delay] [-C call_cost] [-f free_core_index] [-c number_of_calls_per_object] [-cW ClusterWaitTime] [-rP] [-rN ReapedNodes] [-jL JLogger] [-cP ContentionPolicy]
	-b      benchmark, must come before execution model, default [$Bench]
	-n	number of nodes, default [$Nodes]
	-r	read transactions percentage, default [$Reads]
	-o	number of objects, default [$Objects]
	-t	number of threads per node, default [$Threads]
	-x	number of transactions per thread, default [$Transactions]
	-v	verbose mode, enbale logging, default [$Verbose]
	-d	debug mode, log to file, default [$Debug]
	-s	make a sanity check at the end of benchmark (if supported), default [$Sanity]
	-i	enable code instrumentation, must come before execution model, default [$instrument]
	-D	Degree of Quorum Tree: Child node Count
	-M  number of participating machines in the experiment, default [$Machines]
	-I	index of current machine, used with -M option for more than single machine, default [$Machine_Index]
	-T	timeout period in milliseconds, default [$Timeouts]
	-L	transaction processing dealy in milliseconds, default [$TransactionLength]
	-K	network link dealy in milliseconds, deafult [$LinkDelay]
	-C	method call execution cost due to parameter serialization or nodal processing, default [$CallCost]
	-f	index of first avaiable core at machine, used at cores > 24, default [$FreeCore]
	-c	number of calls per object, useful for RMI and RW models, default [$Calls]
	-cW Cluster Wait Time: For which a nodes waits to other nodes to join full cluster
	-rP Network Failure Simulation Enabled
	-rN List of nodes to be Failed e.g. 10-5-8
	-jL JLogger Error reporting level e.g. fatal, debug or info
	-cP Contention Policy to be used by QRTM
			"
		exit 1;;
	esac
done

#echo "Reading Values Done"

temp=$FreeCore
#echo "Running BenchMark"
for ((  i = 0 ;  i < Nodes;  i++  ))
do
	#echo "In for Loop"
    if (( i%Machines == Machine_Index ))
    then 
	echo "Starting [$i]"
	`java -Xmx512m -Djava.security.policy=./QR/no.policy -Djava.net.preferIPv4Stack=true \
	  -DVerbose=$Verbose -DDebug=$Debug -Dinstrument=$instrument \
	  -DSanity=$Sanity -Djgroups.bind_addr=site_local \
	  -DCallcost=$CallCost -DLinkDelay=$LinkDelay -DClusterWaitTime=$ClusterWaitTime \
	  -DTimeouts=$Timeouts -DTransactionLength=$TransactionLength \
	  -DThreads=$Threads -DDegree=$Degree -DReap=$Reap -DJLogger=$JLogger \
	  -DContentionPolicy=$ContentionPolicy -DReapedNodes=$ReapedNodes \
	  -DNodes=$Nodes -DObjects=$Objects -DCalls=$Calls -DTransactions=$Transactions -DReads=$Reads \
	  -jar ./QR/QR.jar $Bench -` &
	p=`ps -ef|grep "${Bench} -"| grep -v 'grep'|awk '{print $1}'`
#	echo taskset -c -p $FreeCore $p
#	taskset -c -p $FreeCore $p
	((FreeCore++))
	if ((FreeCore==24+temp))
	then
		FreeCore=$temp
	fi
    fi
done

#Wait for backgroup processes to finish
c=1
while (( c > 0 ))
do
    sleep 30
    c=`ps -ef|grep "${Bench}"| grep -c -v 'grep'`
done
